package com.allenkerr.handler;

import com.allenkerr.enums.ResultEnum;
import com.allenkerr.exception.GirlException;
import com.allenkerr.util.ResultUtils;
import com.allenkerr.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by 小柯(Allen Kerr) on 17.6.4 21:14.
 */
@RestControllerAdvice
public class ExceptionHandle {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)  //指定处理哪些异常信息
    public Result handle(Exception e) {
        if (e instanceof GirlException) {//年龄太小
            GirlException girlException = (GirlException) e;
            return ResultUtils.error(girlException.getCode(), girlException.getMessage());
        }
        //其他异常
        LOGGER.error("【系统异常】", e);
        return ResultUtils.error(ResultEnum.UNKNOWN_ERROR);
    }

    /*public Result handler(Exception e) {
        LOGGER.error("【系统异常】", e);
        return ResultUtils.error(1, e.getMessage());
    }*/

}
